package com.nami.conver.infra.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nami.conver.domain.pchat.model.ConverModel;
import com.nami.conver.domain.repository.ConverRepository;
import com.nami.conver.infra.orm.model.ConversationDO;
import com.nami.conver.infra.convertor.ConverConvert;
import com.nami.conver.infra.mapper.ConversationMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class ConverRepositoryImpl implements ConverRepository {

    private final ConversationMapper baseMapper;

    public ConverModel getConversation(String appKey, String userId, String targetId) {
        if (StringUtils.isBlank(appKey) || StringUtils.isBlank(userId) || StringUtils.isBlank(targetId)) {
            return null;
        }
        ConversationDO exist = baseMapper.selectOne(new LambdaQueryWrapper<ConversationDO>()
            .eq(ConversationDO::getAppKey, appKey)
            .eq(ConversationDO::getUserId, userId)
            .eq(ConversationDO::getTargetId, targetId)
        );
        if (exist == null) {
            return null;
        }
        return ConverConvert.INSTANCE.convert(exist);
    }

    @Override
    public List<ConverModel> qryConversByGrpMembers(String appKey, List<String> userIds, String targetId) {
        if (StringUtils.isBlank(appKey) || CollUtil.isEmpty(userIds) || StringUtils.isBlank(targetId)) {
            return null;
        }
        List<ConversationDO> conversations = baseMapper.selectList(new LambdaQueryWrapper<ConversationDO>()
            .eq(ConversationDO::getAppKey, appKey)
            .in(ConversationDO::getUserId, userIds)
            .eq(ConversationDO::getTargetId, targetId)
        );

        return ConverConvert.INSTANCE.convertList(conversations);
    }


}
